/**
* @file        bsp_key.h
* @brief       This file contains all the functions prototypes for the KEY. 
* @author      ChipSea MCU Group
* @version     1.0.0
* @date        2023.04.12
* @copyright   CHIPSEA TECHNOLOGIES (SHENZHEN) CORP.
* @note        
****************************************************************************************************
* @par Modify record:
* <tr><th>Date          <th>Version    <th>Author     <th>Description
* <tr><td>2023.04.12    <td>1.0.0      <td>ChipSea    <td>
* -# 
* </table>
*
****************************************************************************************************
*/

#ifndef __KEY_H__
#define __KEY_H__

#include "cs32f10x.h"

/*****************************************Used definition************************************************/


#define KEY_ON  1
#define KEY_OFF 0

#define KEY1_DOWNSTATE 1        //High level when key is pressed

/* KEY GPIO definiton */
#define KEY1_PIN                  GPIO_PIN_13                 
#define KEY1_GPIO_PORT            GPIOC                      
#define KEY1_GPIO_CLK             RCU_APB2_PERI_GPIOC


void Key1_GPIO_Config(void);
uint8_t GetPinStateOfKey1(void);

/*****************************************Used definition************************************************/


#define USER_DATA_EN        0

#define KEY_WOBBLE_TIME     20
 
#define KEY_FIXED_PERIOD    10

/* callback type */
typedef  uint8_t    (*KEY_CALLBACK_PTR)(void);

#define KEY_TIMES_MAX (0XFF)
typedef enum{
    KEY_ACCESS_READ = 0,
    KEY_ACCESS_WRITE_CLEAR = 0x01
}ACCESS_TYPE;




typedef enum
{
    KEY_DOWN        = 1,
    KEY_UP          = 2,
    KEY_UP_WOBBLE   = 3,
    KEY_DOWN_WOBBLE = 4 
}KEY_STATE;

typedef enum
{
    CHANGED         = 1,
    NOT_CHANGED     = 2,
}KEY_STATE_CHAGE;

typedef struct
{
    KEY_CALLBACK_PTR    GetStatePtr;    //Get key pin status 
    uint8_t             Times;          //Increment after key pressed
    KEY_STATE           State;
    KEY_STATE_CHAGE     StateChange;                      
#ifdef KEY_FIXED_PERIOD
    unsigned char       Time_ms;        //time base
#endif
  
#if USER_DATA_EN>0             
    uint8_t             User_Data;      //user data
#endif
  
}KEY;

extern KEY Key1;

void KeyCreate(KEY *p_Key,KEY_CALLBACK_PTR p_CallBack);
void Key_RefreshState(KEY* theKey);
uint8_t Key_AccessTimes(KEY* p_Key,ACCESS_TYPE opt);
uint8_t Key_AccessState(KEY* p_Key,KEY_STATE *p_State);
#endif
